﻿
@{
    ViewBag.Title = "MonthReportIndex";
    Layout = "~/Views/Shared/_Form.cshtml";
}

<div class="rows">
    <div class="panel panel-default">
        <div class="panel-heading navb-bg">
            筛选条件
        </div>
        <table class="form">
            <tr>
                <th class="formTitle">年份：</th>
                <td class="formValue">
                    <select id="year" name="year">
                        <option value="">==请选择==</option>
                    </select>
                </td>
                <th class="formTitle">月份：</th>
                <td class="formValue">
                    <select id="month" name="month">
                        <option value="">==请选择==</option>
                    </select>
                </td>
                <td class="formValue" style="padding-left:20px;">
                    <input type="button" id="btn_search" class="btn btn-primary btn-md" value="查询" style="width:55px" />
                </td>
                <td class="formValue"></td>
                <td class="formValue"></td>
                <td class="formValue"></td>
            </tr>
        </table>
    </div>
</div>
<div id="divNavNew" class="rows hide">
    <div class="panel panel-default">
        <div class="panel-body">
            <span id="span"></span>固定报表尚未生成，点击<a id="btnToNewReport" class="text-primary">生成新报表</a>
        </div>
        <div></div>
    </div>
</div>

<div class="gridPanel">
    <table id="gridList"></table>
</div>

@Html.Partial("_BottomButtonsView", new Newtouch.HIS.Web.Core.Models.BottomButtonViewModel
{
    ShowKeyList = new[] { 6, 7, 9 },
    F6Text = "生成报表",
    F7Text = "导出Excel",
    F9Text = "重新生成",
})

<script type="text/javascript">
    window.newtouch_globalconfig.f4opions = {
        container: "#aaaaaaaaaaaaaaaaaaaa"  //None
    };

    var details = [];
    var isregenerate = false;

    $('#year').select2({ minimumResultsForSearch: -1 });
    $('#month').select2({ minimumResultsForSearch: -1 });

    function initGridList(dlColArr) {
        var colModelArr = [
                {
                    label: "病人类型", name: "patientType", width: 60, align: "left", formatter: function (cellvalue) {
                        return cellvalue == "outpatient" ? "门诊" : (cellvalue == "inpatient" ? "住院" : "");
                    }
                },
                { label: "病历号", name: "blh", width: 80, align: "left" },
                {
                    label: "住院号/门诊号", name: "zyhmzh", width: 120, align: "left", formatter: function (cellvalue) {
                        return cellvalue === "0" ? "" : cellvalue;
                    }
                },
                { label: "病人姓名", name: "brxm", width: 80, align: "left" },
        ];
        if (dlColArr && dlColArr.length && dlColArr.length > 0) {
            $.each(dlColArr, function () {
                colModelArr.push(this);
            });
        }
        //声明 项目明细 grid
        $("#gridList").newtouchLocalDataGrid({
            height: $(window).height() - 190,
            unwritten: false,
            colModel: colModelArr,
            caption: '月报表',
        });
    }
    initGridList();

    $('#btn_search').click(function () {
        newtouch_event_f4();
        var year = $('#year').val();
        var month = $('#month').val();
        if (year && month) {
            $.najax({
                url: '/ReportManage/Report/MonthReportConfirmedData',
                data: { year: year, month: month },
                loading: true,
                success: function (ajaxresp) {
                    if (!!ajaxresp.data && ajaxresp.data && ajaxresp.data.length > 0) {
                        $('#divNavNew').addClass('hide');
                        //报表已生成过 呈现之
                        formatRespData(ajaxresp.data);
                        //报表生成日期
                        var caption = year + '年' + $.zeroPrefixInteger(month, 2) + '月月报表';
                        caption += '（报表已固定 生成日期：' + $.getDate({ date: new Date(ajaxresp.data[0].CreateTime) });
                        caption += ' 费用合计：';
                        caption += getFeeTotal(ajaxresp.data).toFixed(2);
                        caption += '）';
                        $("#gridList").setCaption(caption);

                        $('#btn_bottombutton_f9').removeAttr('disabled');   //重新生成 按钮 可操作
                        $('#btn_bottombutton_f7').removeAttr('disabled');   //导出Excel 可操作
                    }
                    else {
                        $('#span').html(year + '年' + $.zeroPrefixInteger(month, 2) + '月');
                        $('#divNavNew').removeClass('hide');
                    }
                }
            });
        }
    });

    $('#year, #month').change(function () {
        if (details && details.length && details.length > 0) {
            $('#btn_search').trigger('click');
        }
        else {
            newtouch_event_f4();
        }
    });

    $('#btnToNewReport').click(function () {
        newtouch_event_f4();
        loadRealTimeData();
    });

    function loadRealTimeData() {
        var year = $('#year').val();
        var month = $('#month').val();
        if (year && month) {
            $.najax({
                url: '/ReportManage/Report/MonthReportRealTimeData',
                data: { year: year, month: month },
                loading: true,
                success: function (ajaxresp) {
                    if (!!ajaxresp.data && ajaxresp.data.length && ajaxresp.data.length > 0) {
                        formatRespData(ajaxresp.data);

                        var caption = year + '年' + $.zeroPrefixInteger(month, 2) + '月月报表';
                        caption += '（尚未固定 费用合计：';
                        caption += getFeeTotal(ajaxresp.data).toFixed(2);
                        caption += '）';
                        $("#gridList").setCaption(caption);

                        $('#btn_bottombutton_f6').removeAttr('disabled');   //生成报表 按钮 可操作
                    }
                    else {
                        //获取数据失败
                        $.modalAlert("未能提取到当月任何费用<br/>报表生成失败", 'error');
                    }
                },
                error: function () {
                    //获取数据失败
                    $.modalAlert("程序发生异常<br/>报表生成失败", 'error');
                }
            });
        }
    }

    function newtouch_event_f4() {
        details = null;
        isregenerate = false;
        $('#divNavNew').addClass('hide');
        $('.gridPanel').html('<table id="gridList"></table>');  //先清除掉grid的内容
        initGridList();
        $('#btn_bottombutton_f6').attr('disabled', 'disabled');
        $('#btn_bottombutton_f9').attr('disabled', 'disabled');
        $('#btn_bottombutton_f7').attr('disabled', 'disabled');
    }
    $('#btn_bottombutton_f6').attr('disabled', 'disabled');
    $('#btn_bottombutton_f9').attr('disabled', 'disabled');
    $('#btn_bottombutton_f7').attr('disabled', 'disabled');

    function formatRespData(data) {
        details = data;
        var dlColArr = [];
        $.each(data, function () {
            var isContains = false;
            if (this.dl) {
                for (var iiii = 0; iiii < dlColArr.length ; iiii++) {
                    if (dlColArr[iiii].name == this.dl) {
                        isContains = true;
                        break;
                    }
                }
                if (!isContains) {
                    dlColArr.push({
                        label: this.dlmc, name: this.dl, width: 80, align: "left", formatter: "number"
                    });
                }
            }
        });
        $('.gridPanel').html('<table id="gridList"></table>');  //先清除掉grid的内容
        initGridList(dlColArr);
        //呈现数据
        //处理data 至 目标格式（有大类列）
        $("#gridList").newtouchLocalDataGrid({ posttofirst: false, actions: 'addRowData' }, formatDlData(data));
        //呈现data中补差哪一行
        var buchaRow = $.jsonWhere(data, function (v) {
            return !!v.bz && !!v.dlje;    //这是一补差记录
        });
        if (buchaRow && buchaRow.length && buchaRow.length == 1) {
            buchaRow = buchaRow[0];
            //插入到gridList最后一行
            $("#gridList").newtouchLocalDataGrid({ posttofirst: false, actions: 'addRowData' }, [
                { zyhmzh: '补差：' + buchaRow.dlje.toFixed(2), brxm: '调整原因：' + buchaRow.bz }]);
            var $tds = $('#gridList tr:last td:visible:gt(3)').not(':first');
            var tdslength = $tds.length;
            $('#gridList tr:last td:visible:eq(4)').attr('colspan', (tdslength + 1));
            $tds.remove();
        }
        //本月合计：
        //插入到gridList最后一行
        $("#gridList").newtouchLocalDataGrid({ posttofirst: false, actions: 'addRowData' }, [
            { zyhmzh: '本月费用合计：' + getFeeTotal(data).toFixed(2) }]);
        var $tds = $('#gridList tr:last td:visible:gt(2)').not(':first');
        var tdslength = $tds.length;
        $('#gridList tr:last td:visible:eq(3)').attr('colspan', (tdslength + 1));
        $tds.remove();
    }

    //resp data to data
    //处理data 至 目标格式（有大类列）
    function formatDlData(data) {
        var respData = [];
        $.each(data, function () {
            if (this.dl) {
                for (var iiii = 0; iiii < respData.length ; iiii++) {
                    if (respData[iiii].patientType === this.patientType
                        && respData[iiii].blh === this.blh
                        && respData[iiii].zyhmzh === this.zyhmzh
                        && respData[iiii].brxm === this.brxm) {
                        respData[iiii][this.dl] = this.dlje;
                        return; //return， 而不是continue
                    }
                }
                var newPatientData = { patientType: this.patientType, blh: this.blh, zyhmzh: this.zyhmzh, brxm: this.brxm };
                newPatientData[this.dl] = this.dlje;
                respData.push(newPatientData);
            }
        });
        return respData;
    }

    //F6生成报表 固化 调整 浮层页面
    function newtouch_event_f6() {
        var year = $('#year').val();
        var month = $('#month').val();
        if (!details || !details.length || details.length == 0) {
            $.modalAlert("未获取到费用", 'error');
            return;
        }
        $.modalOpen({
            id: "MonthReportDataConfirmPreview",
            title: year + '年' + $.zeroPrefixInteger(month, 2) + '月' + '报表确认',
            url: '/ReportManage/Report/MonthReportDataConfirmPreview?year=' + year + '&month=' + month + '&isregenerate=' + isregenerate + '&calcZje='
                + getFeeTotal(details),
            width: "450px",
            height: "350px",
            callBack: function (iframeId) {
                top.frames[iframeId].submitMonthReport();
            }
        });
    };

    //F9 请求重新生成报表
    function newtouch_event_f9() {
        $.modalConfirm("确定重新生成", function (flag) {
            if (flag) {
                isregenerate = true;
                $('#btn_bottombutton_f9').attr('disabled', 'disabled');   //先让其disabled
                $('#btn_bottombutton_f7').attr('disabled', 'disabled');    //先让其disabled
                loadRealTimeData();
            }
        });
    }

    function getFeeTotal(data) {
        var total = 0;
        $.each(data, function () {
            total = $.addNum(total, this.dlje);
        });
        return total;
    }

    function init() {
        //年月下拉初始化
        var date = new Date;
        //年份
        $("#year").newtouchBindSelect({
            datasource: function () {
                var resultObjArr = new Array();
                if (top.clients.yearArr && top.clients.yearArr.length) {
                    $.each(top.clients.yearArr, function () {
                        resultObjArr.push({ id: this, text: this });
                    });
                }
                return resultObjArr;
            },
            selectedValue: 1900 + date.getYear(),
        });
        //月份
        $("#month").newtouchBindSelect({
            datasource: function () {
                var resultObjArr = new Array();
                if (top.clients.monthArr && top.clients.monthArr.length) {
                    $.each(top.clients.monthArr, function () {
                        resultObjArr.push({ id: this, text: parseInt(this) < 10 ? "0" + this : this });
                    });
                }
                return resultObjArr;
            },
            selectedValue: date.getMonth() + 1,
        });

    }
    init();
</script>
